﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using System.Linq.Dynamic.Core;

using KDS.Mes.Mes.TestImss.Dtos;
using KDS.Mes.Dto;
using KDS.Mes.Authorization;

namespace KDS.Mes.Mes.TestImss
{
    /// <summary>
    /// IMS检测数据表服务实现
    /// </summary>
    [AbpAuthorize(AppPermissions.Pages_Report_TestImss)]
    public class TestImsAppService : MesAppServiceBase, ITestImsAppService
    {
        private readonly IRepository<TestIms, int> _testImsRepository;
        private readonly ITestImsListExcelExporter _testImsListExcelExporter;


        private readonly TestImsManage _testImsManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public TestImsAppService(IRepository<TestIms, int> testImsRepository,
TestImsManage testImsManage
      , ITestImsListExcelExporter testImsListExcelExporter
  )
        {
            _testImsRepository = testImsRepository;
            _testImsManage = testImsManage;
            _testImsListExcelExporter = testImsListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<TestIms> _testImsRepositoryAsNoTrack => _testImsRepository.GetAll().AsNoTracking();


        #endregion


        #region IMS检测数据表管理

        public PagedResultDto<TestImsListDto> GetPagedTestImss(GetTestImsInput input)
        {
            //初步过滤
            var query = _testImsRepository.GetAll()
                        .WhereIf(!input.SeatID.IsNullOrEmpty(), u => u.SEAT_ID == input.SeatID)
                        .WhereIf(!input.LineId.IsNullOrEmpty(), u => u.LINE == input.LineId)
                        .WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.CREAT_TIME >= input.Time1)
                        .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.CREAT_TIME <= input.Time2);
            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CREAT_TIME);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();
            return new PagedResultDto<TestImsListDto>(tasksCount, taskList.MapTo<List<TestImsListDto>>());
        }


        /// <summary>
        /// 根据查询条件获取IMS检测数据表分页列表
        /// </summary>
        public async Task<PagedResultDto<TestImsListDto>> GetPagedTestImssAsync(GetTestImsInput input)
        {

            var query = _testImsRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var testImsCount = await query.CountAsync();

            var testImss = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var testImsListDtos = testImss.MapTo<List<TestImsListDto>>();
            return new PagedResultDto<TestImsListDto>(
            testImsCount,
            testImsListDtos
            );
        }

        /// <summary>
        /// 通过Id获取IMS检测数据表信息进行编辑或修改 
        /// </summary>
        public async Task<GetTestImsForEditOutput> GetTestImsForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetTestImsForEditOutput();

            TestImsEditDto testImsEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _testImsRepository.GetAsync(input.Id.Value);
                testImsEditDto = entity.MapTo<TestImsEditDto>();
            }
            else
            {
                testImsEditDto = new TestImsEditDto();
            }

            output.TestIms = testImsEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取IMS检测数据表ListDto信息
        /// </summary>
        public async Task<TestImsListDto> GetTestImsByIdAsync(EntityDto<int> input)
        {
            var entity = await _testImsRepository.GetAsync(input.Id);

            return entity.MapTo<TestImsListDto>();
        }







        /// <summary>
        /// 新增或更改IMS检测数据表
        /// </summary>
        public async Task CreateOrUpdateTestImsAsync(CreateOrUpdateTestImsInput input)
        {
            if (input.TestImsEditDto.Id.HasValue)
            {
                await UpdateTestImsAsync(input.TestImsEditDto);
            }
            else
            {
                await CreateTestImsAsync(input.TestImsEditDto);
            }
        }

        /// <summary>
        /// 新增IMS检测数据表
        /// </summary>
        public virtual async Task<TestImsEditDto> CreateTestImsAsync(TestImsEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<TestIms>();

            entity = await _testImsRepository.InsertAsync(entity);
            return entity.MapTo<TestImsEditDto>();
        }

        /// <summary>
        /// 编辑IMS检测数据表
        /// </summary>
        public virtual async Task UpdateTestImsAsync(TestImsEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _testImsRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _testImsRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除IMS检测数据表
        /// </summary>
        public async Task DeleteTestImsAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _testImsRepository.DeleteAsync(input.Id);
        }

        #endregion
        #region IMS检测数据表的Excel导出功能


        public async Task<FileDto> GetTestImsToExcel(GetTestImsInput input)
        {
            var entities = await _testImsRepository.GetAll()
                 .WhereIf(!input.SeatID.IsNullOrEmpty(), u => u.SEAT_ID == input.SeatID)
                        .WhereIf(!input.LineId.IsNullOrEmpty(), u => u.LINE == input.LineId)
                        .WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.CREAT_TIME >= input.Time1)
                        .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.CREAT_TIME <= input.Time2)
                .ToListAsync();

            var dtos = entities.MapTo<List<TestImsListDto>>();

            var fileDto = _testImsListExcelExporter.ExportTestImsToFile(dtos);



            return fileDto;
        }


        #endregion










    }
}
